<template>
<div>
  <el-dialog
    :title="title"
    size="small"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime"
                :rules="[
                  {required: true, message:'开始时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      v-model="inputForm.startTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      @focus="showYear(0)"
                      placeholder="选择开始时间"
                      :picker-options="pickerOptionsStart">

                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime"
                :rules="[
                  {required: true, message:'结束时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      v-model="inputForm.endTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择结束时间"
                      :picker-options="pickerOptionsEnd">
                    </el-date-picker>
           </el-form-item>
        </el-col>

        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks">
              <el-input
                v-model="inputForm.remarks"
                type="text"
                style="width: 100%;"
                placeholder="填写备注信息">
              </el-input>
           </el-form-item>
        </el-col>
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="handwritingTimeTab">
            <el-tab-pane label="修改暂停时间">
                  <el-button size="small" @click="addHandwritingAttachedTimeRow" type="primary">新增</el-button>
                  <el-button type="success" @click="refreshAllList()" size="small" >历史记录</el-button>
                  <p style="float: right;color: #a8a8a8;font-size: 18px">tips:暂停时间是用来在开放时间内短暂不可进入评价模块！</p>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.handwritingAttachedTimeList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
          <!--    <el-table-column
                    prop="handwritingTime.id"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="业务主表ID">
                  </el-table-column> -->
                <el-table-column
                    prop="startTime"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label=" 开始时间">
                  </el-table-column>
                <el-table-column
                    prop="endTime"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="结束时间">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewHandwritingAttachedTimeRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button  @click="editHandwritingAttachedTimeRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button  @click="delHandwritingAttachedTimeRow(scope.row)" type="text" size="small">删除</el-button>

                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <HandwritingAttachedTimeForm ref="handwritingAttachedTimeForm" @addRow="saveHandwritingAttachedTimeRow(arguments)"></HandwritingAttachedTimeForm>
</div>
</template>

<script>
  import HandwritingAttachedTimeForm from './HandwritingAttachedTimeForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        handwritingTimeTab: '0',
        inputForm: {
          id: '',
          handwritingAttachedTimeList: [],
          startTime: '',
          remarks: '',
          endTime: ''
        },
        // 限制时间范围
        // 活动开始时间不能选择今天之前的时间 今天之前的时间设置禁止选择状态
        pickerOptionsStart: {
          disabledDate: (time) => {
            if (this.inputForm.endTime) {
              return (
               // 设置选择今天以及今天之后的日期   8.64e7就是一天的时间戳
                time.getTime() < Date.now() - 8.64e7 ||
                time.getTime() > this.inputForm.endTime
                )
            }
            return time.getTime() < Date.now() - 8.64e7
          }
        },
      // 活动结束时间不能选择活动开始的之前的时间
        pickerOptionsEnd: {
          disabledDate: (time) => {
            return (
              // 设置选择今天以及今天之后的日期
              time.getTime() < Date.now() - 8.64e7 ||
              time.getTime() < new Date(this.inputForm.startTime).getTime()
            )
          }
        }
      }
    },
    components: {
      HandwritingAttachedTimeForm
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建开放时间`
        } else if (method === 'edit') {
          this.title = '修改开放时间'
        } else if (method === 'view') {
          this.title = '查看开放时间'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.handwritingTimeTab = '0'
          this.inputForm.handwritingAttachedTimeList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/handwritingtime/handwritingTime/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.handwritingTime)
              this.loading = false
            })
          }
        })
      },

      saveHandwritingAttachedTimeRow (child) {
        if (child[0] === '') {
          this.inputForm.handwritingAttachedTimeList.push(child[1])
        } else {
          this.inputForm.handwritingAttachedTimeList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.handwritingAttachedTimeList.splice(index, 1, child[1])
            }
          })
        }
      },
      addHandwritingAttachedTimeRow (child) {
        this.$refs.handwritingAttachedTimeForm.init('add')
      },
      viewHandwritingAttachedTimeRow (child) {
        this.$refs.handwritingAttachedTimeForm.init('view', child)
      },
      editHandwritingAttachedTimeRow (child) {
        this.$refs.handwritingAttachedTimeForm.init('edit', child)
      },
      delHandwritingAttachedTimeRow (child) {
        this.inputForm.handwritingAttachedTimeList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.handwritingAttachedTimeList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.handwritingAttachedTimeList.splice(index, 1, item)
          }
        })
      },

      refreshAllList (id) {
        this.loading = true
        this.$http({
          url: '/handwritingtime/handwritingTime/atlistall',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
            this.id = this.dataList[0].id
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/handwritingtime/handwritingTime/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
